package com.example.peibei.ui.activity;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.alipay.sdk.app.OpenAuthTask;
import com.dingtao.common.bean.PayBanlance;
import com.dingtao.common.bean.PayOrder;
import com.dingtao.common.bean.TransInfo;
import com.dingtao.common.bean.Wallet;
import com.dingtao.common.core.DataCall;
import com.dingtao.common.core.WDActivity;
import com.dingtao.common.core.exception.ApiException;
import com.dingtao.common.util.SPUtils;
import com.dingtao.common.util.UIUtils;
import com.example.peibei.R;
import com.example.peibei.base.SpConstant;
import com.example.peibei.service.presenter.BindAliPayPresenter;
import com.example.peibei.service.presenter.GetTransInfoPresenter;
import com.example.peibei.service.presenter.MyWalletPresenter;
import com.example.peibei.service.presenter.SubmitTransInfoPresenter;
import com.example.peibei.ui.weight.CEditText;
import com.example.peibei.ui.weight.CenterDialog;
import com.linxiao.framework.dialog.BottomDialog;

import java.lang.ref.WeakReference;
import java.lang.reflect.Field;
import java.util.HashMap;
import java.util.Map;

import butterknife.BindView;
import butterknife.OnClick;
import lombok.With;

public class WithdrawalActivity extends WDActivity {
    @BindView(R.id.tv_money_have)
    TextView money;
    @BindView(R.id.tv_cache)
    TextView tv_cache;
    @BindView(R.id.et_money)
    EditText et_money;
    @BindView(R.id.bt_submit)
    Button bt_submit;

    private SPUtils spUtils;
    private String token;
    private String zfbId;
    private String wxId;
    private long mid;
    private String type;
    private String chooseType = "2";
    private GetTransInfoPresenter getTransInfoPresenter;
    private SubmitTransInfoPresenter submitTransInfoPresenter;
    private String tranMoney;
    private MyWalletPresenter myWalletPresenter;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_withdrawl;
    }

    @Override
    protected void initView() {
        spUtils = new SPUtils(WithdrawalActivity.this, SpConstant.USER_TOKEN);
        token = spUtils.getString(SpConstant.USER_TOKEN);
        zfbId = spUtils.getString(SpConstant.ZFB_ID);
        wxId = spUtils.getString(SpConstant.WX_ID);
        mid = spUtils.getLong(SpConstant.USER_MID);
        myWalletPresenter = new MyWalletPresenter(new WalletCall());
        getTransInfoPresenter = new GetTransInfoPresenter(new TransInfoCall());
        submitTransInfoPresenter = new SubmitTransInfoPresenter(new TransCall());
        myWalletPresenter.reqeust("Bearer " + token, mid);
    }

    @Override
    protected void onResume() {
        super.onResume();
        zfbId = spUtils.getString(SpConstant.ZFB_ID);
        wxId = spUtils.getString(SpConstant.WX_ID);
    }

    class WalletCall implements DataCall<Wallet> {

        @Override
        public void success(Wallet data, Object... args) {
            money.setText("￥" + data.getBalance());
        }

        @Override
        public void fail(ApiException e, Object... args) {
        }
    }

    class TransInfoCall implements DataCall<TransInfo> {

        @Override
        public void success(TransInfo data, Object... args) {
            showDialog(data);
        }

        @Override
        public void fail(ApiException e, Object... args) {
            UIUtils.showToastSafe(e.getDisplayMessage());
        }
    }

    class TransCall implements DataCall<String> {

        @Override
        public void success(String data, Object... args) {
            myWalletPresenter.reqeust("Bearer " + token, mid);
            UIUtils.showToastSafe("提现成功");
        }

        @Override
        public void fail(ApiException e, Object... args) {
            UIUtils.showToastSafe(e.getDisplayMessage());
        }
    }

    private void showTransInfoDialog(TransInfo data) {
        View dialogView = LayoutInflater.from(this).inflate(R.layout.dialog_show_transinfo, null);
        BottomDialog bottomDialog = new BottomDialog(this);
        bottomDialog.setContentView(dialogView);
        bottomDialog.setCanceledOnTouchOutside(true);
        bottomDialog.show();
        Button tv_submit = dialogView.findViewById(R.id.tv_submit);
        ImageView iv_cancel = dialogView.findViewById(R.id.iv_cancel);
        TextView tv_trans_money = dialogView.findViewById(R.id.tv_trans_money);
        TextView tv_fee = dialogView.findViewById(R.id.tv_fee);
        TextView tv_feeScale = dialogView.findViewById(R.id.tv_feeScale);
        tv_trans_money.setText(data.getShowMoney());
        tv_fee.setText("￥" + data.getFee());
        tv_feeScale.setText(data.getFeeScale() + "");
        tv_submit.setOnClickListener(v -> {
            submitTransInfoPresenter.reqeust("Bearer " + token, tranMoney, type);
            bottomDialog.dismiss();
        });
        iv_cancel.setOnClickListener(v -> {
            bottomDialog.dismiss();
        });
    }

    private void showDialog(TransInfo data) {
        View dialogView = LayoutInflater.from(this).inflate(R.layout.dialog_input_pay_password, null);
        CenterDialog bottomDialog = new CenterDialog(this);
        bottomDialog.setContentView(dialogView);
        bottomDialog.setCanceledOnTouchOutside(true);
        bottomDialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
        bottomDialog.show();
        ImageView tv_cancel = dialogView.findViewById(R.id.tv_cancel);
        TextView tv_money = dialogView.findViewById(R.id.tv_money);
        TextView tv_fee = dialogView.findViewById(R.id.tv_fee);
        TextView tv_feeScale = dialogView.findViewById(R.id.tv_feeScale);
        tv_fee.setText("￥" + data.getFee());
        tv_feeScale.setText(data.getFeeScale() + "");
        CEditText cedittext = dialogView.findViewById(R.id.cedittext);
        tv_money.setText("￥" + data.getShowMoney());
        tv_cancel.setOnClickListener(v -> bottomDialog.dismiss());
        cedittext.setOnFinishListener(msg -> {
            submitTransInfoPresenter.reqeust("Bearer " + token, tranMoney, type, msg);
            bottomDialog.dismiss();
        });
//        showInput(cedittext);
    }

    /**
     * 显示键盘
     *
     * @param et 输入焦点
     */
    public void showInput(final EditText et) {
        et.requestFocus();
        InputMethodManager imm = (InputMethodManager) getSystemService(INPUT_METHOD_SERVICE);
        imm.showSoftInput(et, InputMethodManager.SHOW_IMPLICIT);
    }

    @OnClick(R.id.iv_back)
    public void back() {
        finish();
    }

    @OnClick(R.id.tv_order_history)
    public void billRecord() {
        intent(BillRecordActivity.class);
    }

    @OnClick(R.id.bt_submit)
    public void submitTrans() {
        tranMoney = et_money.getText().toString();
        if (TextUtils.isEmpty(type)) {
            UIUtils.showToastSafe("请选择到账方式");
            return;
        }
        if (TextUtils.isEmpty(tranMoney)) {
            UIUtils.showToastSafe("提现金额不能为空");
            return;
        }
        //提现
        getTransInfoPresenter.reqeust("Bearer " + token, tranMoney);
    }

    @OnClick(R.id.rl_choose_type)
    public void choosePay() {
        //判断是否绑定支付宝、微信
        if (zfbId.equals("false")) {
            showBindAccount();
        } else {
            showChoosePayAccount();
        }
    }

    private void showChoosePayAccount() {
        View dialogView = LayoutInflater.from(this).inflate(R.layout.dialog_choose_pay_account, null);
        BottomDialog bottomDialog = new BottomDialog(this);
        bottomDialog.setContentView(dialogView);
        bottomDialog.setCanceledOnTouchOutside(true);
        bottomDialog.show();
        Button tv_submit = dialogView.findViewById(R.id.tv_submit);
        RelativeLayout rl_zfb = dialogView.findViewById(R.id.rl_zfb);
        RelativeLayout rl_wx = dialogView.findViewById(R.id.rl_wx);
        ImageView iv_choose_zfb = dialogView.findViewById(R.id.iv_choose_zfb);
        ImageView iv_choose_wx = dialogView.findViewById(R.id.iv_choose_wx);
        if (!TextUtils.isEmpty(type) && type.equals("1")) {
            iv_choose_zfb.setVisibility(View.INVISIBLE);
            iv_choose_wx.setVisibility(View.VISIBLE);
        } else {
            iv_choose_zfb.setVisibility(View.VISIBLE);
            iv_choose_wx.setVisibility(View.INVISIBLE);
        }
        rl_zfb.setOnClickListener(v -> {
            iv_choose_zfb.setVisibility(View.VISIBLE);
            iv_choose_wx.setVisibility(View.INVISIBLE);
            chooseType = "2";
        });
        rl_wx.setOnClickListener(v -> {
            iv_choose_zfb.setVisibility(View.INVISIBLE);
            iv_choose_wx.setVisibility(View.VISIBLE);
            chooseType = "1";
        });
        tv_submit.setOnClickListener(v -> {
            type = chooseType;
            tv_cache.setText(type.equals("2") ? "支付宝" : "微信");
            bottomDialog.dismiss();
        });
    }

    private void showBindAccount() {
        final AlertDialog.Builder normalDialog =
                new AlertDialog.Builder(WithdrawalActivity.this);
        final View dialogView = LayoutInflater.from(WithdrawalActivity.this)
                .inflate(R.layout.dialog_bind_account, null);
        normalDialog.setView(dialogView);
        TextView tv_cancel = dialogView.findViewById(R.id.tv_cancel);
        TextView tv_sure = dialogView.findViewById(R.id.tv_sure);
        AlertDialog alertDialog = normalDialog.create();
        tv_cancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                alertDialog.cancel();
            }
        });
        tv_sure.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                alertDialog.cancel();
                intent(BindPayAccountActivity.class);
            }
        });
        // 显示
        alertDialog.show();
    }

    @Override
    protected void destoryData() {

    }
}
